package org.jeecg.modules.arc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.arc.entity.ArcContacts;
import org.jeecg.modules.arc.entity.ArcCustomers;
import org.jeecg.modules.arc.mapper.ArcContactsMapper;
import org.jeecg.modules.arc.service.IArcContactsService;
import org.jeecg.modules.arc.service.IArcCustomersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 用户档案
 * @Author: jeecg-boot
 * @Date:   2022-02-19
 * @Version: V1.0
 */
@Service
public class ArcContactsServiceImpl extends ServiceImpl<ArcContactsMapper, ArcContacts> implements IArcContactsService {

    @Autowired
    private ISysBaseAPI baseAPI;
    @Autowired
    private IArcCustomersService customersService;

    @Override
    @Transactional
    public void updateRecord(ArcContacts arcContacts) {

        boolean isModfiy=false;
        ArcContacts oldContacts = baseMapper.selectById(arcContacts.getId());
        ArcCustomers arcCustomers=null;
        List<ArcCustomers> list = customersService.list(new LambdaQueryWrapper<ArcCustomers>()
                .eq(ArcCustomers::getContactId, arcContacts.getId())
                //.eq(ArcCustomers::getCusName, arcContacts.getName())
                .eq(ArcCustomers::getDelFlag, 0));
        if(null != list && list.size()>0){
            arcCustomers = list.get(0);
        }

        String code="402881a9803039020180303902e10000";
        if( (null !=oldContacts.getAddress() && !oldContacts.getAddress().equals(arcContacts.getAddress()))
                || (null == oldContacts.getAddress() && null !=arcContacts.getAddress())
            ){
            isModfiy=true;
            JSONObject jsonObj=new JSONObject();
            jsonObj.put("CUS_ID",oldContacts.getId());
            jsonObj.put("cus_code", arcCustomers!=null ? arcCustomers.getCode():oldContacts.getName());
            jsonObj.put("cus_name",oldContacts.getName());
            jsonObj.put("old_text",oldContacts.getAddress());
            jsonObj.put("new_text",arcContacts.getAddress());
            baseAPI.createOnlineData(code,jsonObj);
        }
        if( (null !=oldContacts.getCertificateId() && !oldContacts.getCertificateId().equals(arcContacts.getCertificateId()))
                || (null == oldContacts.getCertificateId() && null !=arcContacts.getCertificateId())
        ){
            isModfiy=true;
            JSONObject jsonObj=new JSONObject();
            jsonObj.put("cus_id",oldContacts.getId());
            jsonObj.put("cus_code", arcCustomers!=null ? arcCustomers.getCode():oldContacts.getName());
            jsonObj.put("cus_name",oldContacts.getName());
            jsonObj.put("old_text",oldContacts.getCertificateId());
            jsonObj.put("new_text",arcContacts.getCertificateId());
            baseAPI.createOnlineData(code,jsonObj);
        }
        if( (null !=oldContacts.getMobile() && !oldContacts.getMobile().equals(arcContacts.getMobile()))
                || (null == oldContacts.getMobile() && null !=arcContacts.getMobile())
            ){
            isModfiy=true;
            JSONObject jsonObj=new JSONObject();
            jsonObj.put("cus_id",oldContacts.getId());
            jsonObj.put("cus_code", arcCustomers!=null ? arcCustomers.getCode():oldContacts.getName());
            jsonObj.put("cus_name",oldContacts.getName());
            jsonObj.put("old_text",oldContacts.getMobile());
            jsonObj.put("new_text",arcContacts.getMobile());
            baseAPI.createOnlineData(code,jsonObj);
        }
        if(!oldContacts.getName().equals(arcContacts.getName() )){
            isModfiy=true;
            JSONObject jsonObj=new JSONObject();
            jsonObj.put("cus_id",oldContacts.getId());
            jsonObj.put("cus_code", arcCustomers!=null ? arcCustomers.getCode():oldContacts.getName());
            jsonObj.put("cus_name",oldContacts.getName());
            jsonObj.put("old_text",oldContacts.getName());
            jsonObj.put("new_text",arcContacts.getName());
            baseAPI.createOnlineData(code,jsonObj);

//            arcCustomers.setCusName(arcContacts.getName());
            if(null != arcCustomers) {
                customersService.update(new LambdaUpdateWrapper<ArcCustomers>()
                        .set(ArcCustomers::getCusName, arcContacts.getName())
                        .eq(ArcCustomers::getId, arcCustomers.getId()));
//            customersService.updateById(arcCustomers);
            }
        }

        if( (null !=oldContacts.getEmail() && !oldContacts.getEmail().equals(arcContacts.getEmail()))
                || (null == oldContacts.getEmail() && null !=arcContacts.getEmail())
            ){
            isModfiy=true;
            JSONObject jsonObj=new JSONObject();
            jsonObj.put("cus_id",oldContacts.getId());
            jsonObj.put("cus_code", arcCustomers!=null ? arcCustomers.getCode():oldContacts.getName());
            jsonObj.put("cus_name",oldContacts.getName());
            jsonObj.put("old_text",oldContacts.getEmail());
            jsonObj.put("new_text",arcContacts.getEmail());
            baseAPI.createOnlineData(code,jsonObj);
        }
        if( (null !=oldContacts.getTelphone() && !oldContacts.getTelphone().equals(arcContacts.getTelphone()))
                || (null == oldContacts.getTelphone() && null !=arcContacts.getTelphone())
            ){
            isModfiy=true;
            JSONObject jsonObj=new JSONObject();
            jsonObj.put("cus_id",oldContacts.getId());
            jsonObj.put("cus_code", arcCustomers!=null ? arcCustomers.getCode():oldContacts.getName());
            jsonObj.put("cus_name",oldContacts.getName());
            jsonObj.put("old_text",oldContacts.getTelphone());
            jsonObj.put("new_text",arcContacts.getTelphone());
            baseAPI.createOnlineData(code,jsonObj);
        }
        if( (null !=oldContacts.getPostcode() && !oldContacts.getTelphone().equals(arcContacts.getPostcode()))
                || (null == oldContacts.getPostcode() && null !=arcContacts.getPostcode())
            ){
            isModfiy=true;
            JSONObject jsonObj=new JSONObject();
            jsonObj.put("cus_id",oldContacts.getId());
            jsonObj.put("cus_code", arcCustomers!=null ? arcCustomers.getCode():oldContacts.getName());
            jsonObj.put("cus_name",oldContacts.getName());
            jsonObj.put("old_text",oldContacts.getPostcode());
            jsonObj.put("new_text",arcContacts.getPostcode());
            baseAPI.createOnlineData(code,jsonObj);
        }

        if(isModfiy){
            baseMapper.updateById(arcContacts);
        }

    }
}
